import {mapGetters} from "vuex";
import {toLogin} from '@/libs/login.js';
import diyJson from '@/config/diy.js';
import * as AccountInfo from '@/api/member/memberAccountCard.js'
import UvUpload from '@/components/school/uv-upload';

const app = getApp();
export const mixins = {
  computed: mapGetters(['isLogin', 'userInfo']),
  components: {
    UvUpload,
  },
  data() {
    return {

      themConfig: diyJson.commonStyle,
      settlementAccountType: "01",
      formData: {},
      rules: {
        'realName': {
          type: 'string',
          required: true,
          message: '请填写姓名',
          trigger: ['blur', 'change']
        },
        'cardNo': {
          type: 'string',
          max: 18,
          min: 18,
          required: true,
          message: '请填写',
          trigger: ['blur', 'change']
        },
        // 'certStartDate': {
        //   type: 'string',
        //   required: true,
        //   message: '请填写',
        //   trigger: ['blur', 'change']
        // },
        // 'cardFrontImg': {
        //   type: 'string',
        //   required: true,
        //   message: '请上传',
        //   trigger: ['blur', 'change']
        // },
        // 'cardBackImg': {
        //   type: 'string',
        //   required: true,
        //   message: '请上传',
        //   trigger: ['blur', 'change']
        // },
        'bankCardNo': {
          type: 'string',
          required: true,
          message: '请填写',
          trigger: ['blur', 'change']
        },
        'bankName': {
          type: 'string',
          required: true,
          message: '请填写',
          trigger: ['blur', 'change']
        },
        // 'branchName': {
        //   type: 'string',
        //   required: true,
        //   message: '请填写',
        //   trigger: ['blur', 'change']
        // },
        'bankCardPhone': {
          type: 'string',
          max: 11,
          min: 11,
          required: true,
          message: '请填写11位手机号',
          trigger: ['blur', 'change']
        },
        'wechatPaymentCode': {
          type: 'string',
          required: false,
          message: '',
          trigger: ['blur', 'change']
        },
        'alipayPaymentCode': {
          type: 'string',
          required: false,
          message: '',
          trigger: ['blur', 'change']
        }
      },

      calendarField: '',
      calendarFieldValue: 0,
    };
  },
  methods: {
    getUserInfo() {
      // 刷新用户信息
      dispatch('USERINFO');
    },

    // 获取用户卡信息
    getInfo() {
      AccountInfo.getByUserId(this.userInfo.id).then(res => {
        this.formData = res.data === null ? {} : res.data;
      })
    },

    // 上传图片
    uploadSuccess(obj) {
      console.log("上传回调", obj)
      let name = obj.params.name;
      let array = obj.images;
      let photos = [];
      for (let i = 0; i < array.length; i++) {
        photos.push(array[i].url);
      }
      this.$nextTick(() => {
        this.formData[name] = photos.length > 0 ? photos[0] : null;
      });
      console.log("上传成功", this.formData)
    },

    goBack(url) {
      setTimeout(() => {
        // #ifdef H5
        return history.back();
        // #endif

        // #ifndef H5
        return uni.navigateBack({
          delta: 1,
        })
        // #endif
      }, 1000)
    },

    submit() {
      uni.showLoading({
        title: '处理中...',
        mask: true
      });
      // 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
      this.$refs.form.validate().then(res => {
        if (this.formData.id) {
          AccountInfo.updateInfo(this.formData).then(res => {
            uni.showToast({
              icon: 'success',
              title: '已保存，正在返回...'
            })
            uni.hideLoading();
            this.goBack(null);
          })
        } else {
          AccountInfo.addInfo(this.formData).then(res => {
            uni.showToast({
              icon: 'success',
              title: '已保存，正在返回...'
            })
            uni.hideLoading();
            this.goBack(null);
          })
        }
      }).catch(errors => {
        uni.hideLoading();
        console.log("errors", errors, this.formData)
        uni.showToast({
          icon: 'error',
          title: '请将表单填写完整'
        })
      })
    },

    // 打开日历选择
    openDate(field) {
      this.calendarField = field;
      this.$refs.datetimePicker.open();
    },

    // 选择日期
    confirmDate(e) {
      console.log('日历选择：', this.calendarField,
          this.timestampToYMD(e.value));
      let that = this;
      this.$nextTick(() => {
        that.formData[that.calendarField] = that.timestampToYMD(e.value);
      });
      console.log('日历选择：', that.formData);
    },
    timestampToYMD(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = ('0' + (date.getMonth() + 1)).slice(-2);
      const day = ('0' + date.getDate()).slice(-2);
      return `${year}-${month}-${day}`;
    },

    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      if (type === 'day') {
        return `${value}日`
      }
      return value
    },
    getApp() {
      return app;
    },

  },

  onPullDownRefresh() {

  },
  onLoad: function (options) {
    if (!this.isLogin) {
      toLogin();
    }
  },
  onShow() {
    this.getUserInfo();
  },
  onReady() {
    this.getInfo();
  },
  onReachBottom() {
  }
}
